clear
version 13.1
set more off

if "`c(username)'"=="JULIANMA"  { 
	global dir =  "C:\Users\JULIANMA\Dropbox\PaperPrimeroManizales\" 		
}
	
		
	
*** DATA WORK-------------------------------------------------------------------
//import

use "$dir\6.Text\Submissions\JPubE\Harvard Dataverse\data_randomization.dta", clear


set seed 353895791


//blocks of 4
sort nrstudents prop_urban prop_lowstatus avg_age
gen block = int((_n-1)/4) +1


//Randomization in the blocks
  cap drop n rank T
 
  gen n = uniform() 
  egen rank = rank(n), by(block)
  gen T1=rank==1
  gen T2=rank==2
  gen T = T1+T2

  label var T1 "treated school in first round"
  label var T2 "treated school in second round"
 


*** BALANCE (SCHOOL LEVEL)------------------------------------------------------  
global balance = "nrstudents nclass kgclass afternoon prop_boys avg_age  prop_lowstatus prop_urban"

    
  local n: word count $balance
  matrix coeffs = J(`n',3,999)
  matrix rownames coeffs = $balance
  matrix colnames coeffs = beta pval Obs
  
  local i = 1
  foreach var in $balance {
    qui areg `var' T1, absorb(block) robust
    matrix  coeffs[`i',1] = _b[T]
    matrix  coeffs[`i',2] =  (2 * ttail(e(df_r), abs(_b[T]/_se[T])))
 	matrix  coeffs[`i',3] = e(N)
 local i = `i'+1
    }
  mat list coeffs  

	
  local i = 1
  foreach var in $balance {
    qui areg `var' T, absorb(block) robust
    matrix  coeffs[`i',1] = _b[T]
    matrix  coeffs[`i',2] =  (2 * ttail(e(df_r), abs(_b[T]/_se[T])))
 	matrix  coeffs[`i',3] = e(N)
 local i = `i'+1
    }
	
  mat list coeffs  


  local i = 1
  foreach var in $balance {
    qui areg `var' T1 [aw=nrstudents], absorb(block) robust
    matrix  coeffs[`i',1] = _b[T]
    matrix  coeffs[`i',2] =  (2 * ttail(e(df_r), abs(_b[T]/_se[T])))
 	matrix  coeffs[`i',3] = e(N)
 local i = `i'+1
    }
  mat list coeffs  

	
  local i = 1
  foreach var in $balance {
    qui areg `var' T [aw=nrstudents], absorb(block) robust
    matrix  coeffs[`i',1] = _b[T]
    matrix  coeffs[`i',2] =  (2 * ttail(e(df_r), abs(_b[T]/_se[T])))
 	matrix  coeffs[`i',3] = e(N)
 local i = `i'+1
    }
	
  mat list coeffs    
  
   
*** TREATED SCHOOLS (DATA FOR LUKER) -----------------------------------------------------

keep if (T1==1 | T2==1)

keep block schoolid //estudiantes grupo

*export excel sorteo_1ro_sedes_2017_12_12, replace firstrow(variables)
